package com.yunkaweilai.android.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.MainActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.AppRuleModel;
import com.yunkaweilai.android.model.CodeMsgBean;
import com.yunkaweilai.android.model.login.LoginModel;
import com.yunkaweilai.android.model.login.UserInfoModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.CountDownTimerUtils;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.NoTimeDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 安全登录界面，手机验证码登录
 * Created by Administrator on 2018/5/12.
 */

public class SafeLoginActivity extends BaseActivity {
    public static final String SAFE_PHONE = "SAFE_PHONE";
    public static final String SAFE_USERNAME = "SAFE_USERNAME";
    public static final String SAFE_LOGIN_TOKEN = "SAFE_LOGIN_TOKEN";
    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_tv_phone)
    TextView idTvPhone;
    @BindView(R.id.id_edt_code)
    EditText idEdtCode;
    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    @BindView(R.id.id_btn_login)
    Button idBtnLogin;
    private String phone = "";
    private String usernaem = "";
    private String tokens = "";
    private CountDownTimerUtils countDownTimerUtils;
    private Gson gson = new Gson();

    public static void actionStart(Context context, String phone, String username, String SafeLoginToken) {
        Intent intent = new Intent(context, SafeLoginActivity.class);
        intent.putExtra(SAFE_PHONE, phone);
        intent.putExtra(SAFE_LOGIN_TOKEN, SafeLoginToken);
        intent.putExtra(SAFE_USERNAME, username);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_login);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }).setTitleText("安全登录");
        phone = getIntent().getStringExtra(SAFE_PHONE);
        usernaem = getIntent().getStringExtra(SAFE_USERNAME);
        tokens = getIntent().getStringExtra(SAFE_LOGIN_TOKEN);
        countDownTimerUtils = new CountDownTimerUtils(idTvTime, 60000, 1000);
        idTvPhone.setText(phone + "");

        idTvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initGetCode();
            }
        });
        idBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idBtnLogin.setClickable(false);
                initSafeLogin();
            }
        });


    }
    //安全登录
    private void initSafeLogin() {
        showProgressDialog("请稍等，正在登录中！");
        HttpRequest.postUrl(Api.SAFE_LOGIN)
                .addParams("phone",phone)
                .addParams("UserName",usernaem)
                .addParams("SafeLoginToken",tokens)
                .addParams("PhoneVerifyCode",idEdtCode.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        idBtnLogin.setClickable(true);
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            LoginModel loginModel = gson.fromJson(json, LoginModel.class);
                            String token = loginModel.getData().getToken() + "";
                            BaseApplication.getInstance().token = token;
                            initMyInfo();
                        }else{
                            dismissProgressDialog();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        dismissProgressDialog();
                        idBtnLogin.setClickable(true);
                    }
                });
    }

    //获取用户信息
    private void initMyInfo() {
        HttpRequest.postUrl(Api.INIT_USER_INFO)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCodeNoLogin(mAc, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            UserInfoModel userInfoModel = gson.fromJson(json, UserInfoModel.class);
                            BaseApplication.userInfo = userInfoModel.getData().getStoreEmployeeInfo();
                            getAppRule();
                        } else {
                            dismissProgressDialog();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        dismissProgressDialog();
                    }
                });
    }

    //获取权限
    private void getAppRule() {
        HttpRequest.postUrl(Api.APP_RULE)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCodeNoLogin(mAc, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            AppRuleModel appRuleModel = gson.fromJson(json, AppRuleModel.class);
                            BaseApplication.appRuleModel = appRuleModel.getData().getInfo();
                            Intent intent = new Intent(mAc, MainActivity.class);
                            startActivity(intent);
                        }
                        dismissProgressDialog();
                    }

                    @Override
                    public void onError(Exception e) {
                        dismissProgressDialog();
                    }
                });
    }

    //获取验证码
    private void initGetCode() {
        HttpRequest.postUrl(Api.SAFE_GET_CODE)
                .addParams("UserName",usernaem)
                .addParams("employee_tel",phone)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        CodeMsgBean codeMsgBean = gson.fromJson(json, CodeMsgBean.class);
                        if (codeMsgBean.getCode() == 0){
                            showToast(codeMsgBean.getMessage());
                        }else if (codeMsgBean.getCode() == 2){
                            NoTimeDialog dialog = new NoTimeDialog(mAc,codeMsgBean.getMessage());
                            dialog.show();
                        }else{
                            countDownTimerUtils.start();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }
}
